<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>获取手机号</title>
</head>
<body>
  <div id="box"></div>
  <br>
  <div id="device-model"></div>

  <a href="./fp.html">浏览器指纹</a>

  <script src="./js/mobile-detect.min.js"></script>
  <script src="./js/mobile-device.js"></script>
  <script>
    // 获取手机型号
    function phoneModel() {
      let contains = function(deviceInfo, needle) {
        for (let i in deviceInfo) {
          if (deviceInfo[i].indexOf(needle) > 0) return i;
        }
        return -1;
      };
      let device_type = navigator.userAgent; //获取userAgent信息
      document.getElementById('box').innerHTML = `UA: ${device_type}`

      let md = new MobileDetect(device_type); //初始化mobile-detect
      let os = md.os(); //获取系统
      let model = "";
      if (os == "iOS") {
        //ios系统的处理
        os = md.os() + md.version("iPhone");
        ios = md.mobile();
        //再通过iphone-device.js获取具体的苹果手机型号
        newMobile = MobileDevice.getModels().join(' or ');
        console.log(newMobile);
        if(newMobile == 'unknown'){
          model = '';
        }
      } else if (os == "AndroidOS") {
        //Android系统的处理
        os = md.os() + md.version("Android");
        let arr = device_type.split(";");
        let i = contains(arr, "Build/");
        if (i > -1) {
          model = arr[i].substring(0, arr[i].indexOf("Build/"));
        }
      }
      const brand =
        md.mobile() !== "UnknownPhone"
          ? md.mobile()
          : md.phone() !== "UnknownPhone"
          ? md.phone()
          : "";
      // return os == "iOS" ? `${model}(${os})` : `${brand}${model}(${os})`;
      return model
    }

    const deviceModel = phoneModel()
    document.getElementById('device-model').innerHTML = `型号是: ${deviceModel}`
    console.log(rs);
  </script>
</body>
</html>